package com.num2.service.lkw;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import com.num2.dao.lkw.IWithdrawDao;
import com.num2.form.lkw.Withdraw_Money;
import com.num2.form.xjg.LoginForm;

@Service
public class WithdrawService {
	@Resource
	private SqlSessionFactory sqlSessionFactory;
	private LoginForm loginform;
	public boolean deleteMoney(Withdraw_Money wm,HttpSession session) {
		//打开session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过 注解 获取 接口实现类的对象
       IWithdrawDao withdrawdao = sqlSession.getMapper(IWithdrawDao.class);
       
       //通过session得到的username获取user_id
       int userid = withdrawdao.selectName((String) session.getAttribute("user_name"));
       wm.setUser_id(userid);
       
       //通过user_id获取余额balance
       int balanc= withdrawdao.select(wm.getUser_id());
        //将获取的存款金额转变为字符串数组,判断是否每个字符都为数字
           char num[] = wm.getIncome_expend().substring(1).toCharArray();
           for(int i=0;i<num.length;i++){
    			if(!Character.isDigit(num[i])){
    			    return false;
    			}
           }
           //若余额小于取款金额，则取款失败
           if(balanc<Integer.parseInt(wm.getIncome_expend().substring(1))){
        	   return false;
           }
	       wm.setBalance(balanc);
//	       System.out.println(balanc);
	     //增加一条用户取款的记录
	       withdrawdao.delete(wm);
	       //提交session
	       sqlSession.commit();
       return true;
      
	}

	public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public LoginForm getLoginform() {
		return loginform;
	}

	public void setLoginform(LoginForm loginform) {
		this.loginform = loginform;
	}

}
